package com.matrix.java.chap_08.util;

import java.util.Scanner;

public class ArrayUtil {
    public ArrayUtil() {
    }

    /**
     * 使用输入值初始化数组
     *
     * @param rowNum    行数
     * @param columnNum 列数
     * @return int型二维数组
     */
    public int[][] inputInitMatrix(int rowNum, int columnNum) {
        int[][] matrix = new int[rowNum][columnNum];
        Scanner input = new Scanner(System.in);
        System.out.print("Enter elements: ");
        for (int row = 0; row < rowNum; row++) {
            for (int column = 0; column < columnNum; column++) {
                matrix[row][column] = input.nextInt();
            }
        }
        input.close();
        return matrix;
    }
    public double[][] inputInitDoubleMatrix(int rowNum, int columnNum) {
        double[][] matrix = new double[rowNum][columnNum];
        Scanner input = new Scanner(System.in);
        System.out.print("Enter elements: ");
        for (int row = 0; row < rowNum; row++) {
            for (int column = 0; column < columnNum; column++) {
                matrix[row][column] = input.nextDouble();
            }
        }
        input.close();
        return matrix;
    }

    /**
     * 使用随机值(0~num)初始化数组
     *
     * @param rowNum    行数
     * @param columnNum 列数
     * @param num       随机数取数范围
     * @return int型二维数组
     */
    public int[][] randomInitMatrix(int rowNum, int columnNum, int num) {
        int[][] matrix = new int[rowNum][columnNum];
        for (int row = 0; row < rowNum; row++) {
            for (int column = 0; column < columnNum; column++) {
                matrix[row][column] = (int) (Math.random() * num);
            }
        }
        return matrix;
    }

    /**
     * 打印数组
     *
     * @param matrix 数组
     */
    public void printMatrix(int[][] matrix) {
        for (int[] ints : matrix) {
            for (int column = 0; column < matrix[0].length; column++) {
                System.out.print(ints[column] + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 求所有元素的和
     *
     * @param matrix 数组
     * @return 总和
     */
    public int sumMatrix(int[][] matrix) {
        int total = 0;
        for (int[] ints : matrix) {
            for (int column = 0; column < matrix[0].length; column++) {
                total += ints[column];
            }
        }
        return total;
    }

    /**
     * 求数组某一列的和
     *
     * @param matrix    数组
     * @param columnNum 列数
     * @return 列和
     */
    public int sumMatrixColumn(int[][] matrix, int columnNum) {
        int columnTotal = 0;
        for (int[] ints : matrix) {
            columnTotal += ints[columnNum];
        }
        return columnTotal;
    }

    /**
     * 哪一行的和最大,返回int数组,0为索引,1为和
     *
     * @param matrix 数组
     * @return 结果集
     */
    public int[] findMaxRow(int[][] matrix) {
        int[] maxRowArray = new int[2];
        int maxRow = 0;
        int indexOfMaxRow = 0;
        for (int column = 0; column < matrix[0].length; column++) {
            maxRow += matrix[0][column];
        }
        for (int row = 1; row < matrix.length; row++) {
            int totalOfThisRow = 0;
            for (int column = 0; column < matrix[row].length; column++) {
                totalOfThisRow += matrix[row][column];
            }
            if (totalOfThisRow > maxRow) {
                maxRow = totalOfThisRow;
                indexOfMaxRow = row;
            }
        }
        maxRowArray[0] = indexOfMaxRow;
        maxRowArray[1] = maxRow;
        return maxRowArray;
    }

    /**
     * 随意打乱
     *
     * @param matrix
     */
    public void upsetMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                int i1 = (int) (Math.random() * matrix.length);
                int j1 = (int) (Math.random() * matrix[i].length);
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i1][j1];
                matrix[i1][j1] = temp;
            }
        }
    }
}
